#include "sqlite.hpp"
#include <iostream>
#include <cassert>
#include <vector>

const std::string dbfile = "./test.db";

int selectStudentCallback(void* arg, int col_count, char** result, char** fields_name)
{
    std::vector<std::string>* stus = static_cast<std::vector<std::string>*>(arg);
    std::string data = "";
    for(int i = 0; i < col_count; ++i)
    {
        if(result[i]) data += result[i];
        else data += "null";
        data += " ";
    }
    stus->emplace_back(data);
    return 0;
}

int main()
{
    // 1. 创建/打开库文件
    SqliteHelper sqlhelper(dbfile);
    assert(sqlhelper.open());
   
    //2. 建表（不存在则创建）, 学生信息: 学号，姓名，年龄
    // const char* ct_sql = "create table if not exists student(sn int primary key, name varchar(32), age int);";
    // assert(sqlhelper.exec(ct_sql, nullptr, nullptr));

    // const char* inset_sql = "insert into student values(1, '小帅', 18), (2, '小美', 19), (3, '大美', 18);";
    // assert(sqlhelper.exec(inset_sql, nullptr, nullptr));

    // const char* update_sql = "update student set age='20' where sn=1";
    // assert(sqlhelper.exec(update_sql, nullptr, nullptr));    

    // const char* delete_sql = "delete from student where sn=3";
    // assert(sqlhelper.exec(delete_sql, nullptr, nullptr));    

    const char* select_sql = "select * from student;";
    std::vector<std::string> stus;
    assert(sqlhelper.exec(select_sql, selectStudentCallback, (void*)&stus));   
    for (auto &e : stus) std::cout << e << "\n";
    
    // 4. 关闭文件
    sqlhelper.close();
    return 0;
}